package com.example.android.raycast;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;

public class Door {

	public static int DOOR_CLOSED = 0;
	public static int DOOR_OPENING = 1;
	public static int DOOR_OPEN = 2;
	public static int DOOR_CLOSING = 3;
	
	private int TEXTURE_WIDTH = 64;
	private int TEXTURE_HEIGHT = 64;
	private int DOOR_HEIGHT = 64;
	private int DOOR_WIDTH = 64;
	
	private int mDoorType = 0;
	private int mState = DOOR_CLOSED;	
	//private int mX = 0;
	//private int mY = 0;
	private Bitmap[] mBitmaps;
	private boolean mSeen = false;
	private int mWidthOfDoorShowing = TEXTURE_WIDTH;
	private int mOpenCloseSpeed = 2;
	private boolean mIsExit = false;
		
	private int mSeenAtFirstColumn = 1000;
	private int mSeenAtLastColumn = 0;
	private float mSeenAtDistance = Float.MAX_VALUE;
	
    private static BitmapFactory.Options sBitmapOptions 
    = new BitmapFactory.Options();
		
	public Door(int type, Context context, boolean isExit) {
		sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
		
		//note this is actual position, not grid position
		//mX = X;
		//mY = Y;
			
		//The number in the world map array used for this door (eg 15)
		mDoorType = type;
		
		//Whether the door is an exit door or not
		mIsExit = isExit;
		
		mBitmaps = new Bitmap[1];
		mBitmaps[0] = loadBitmap(context, R.drawable.steel_door);
		
		
		
	}
	
	public int getState() {
		return mState;
	}
	
	public boolean getSeen() {
		return mSeen;
	}
	
    protected Bitmap loadBitmap(Context context, int resourceId) {
        Bitmap bitmap = null;
        if (context != null) {
          
            InputStream is = context.getResources().openRawResource(resourceId);
            try {
                bitmap = BitmapFactory.decodeStream(is, null, sBitmapOptions);
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore.
                }
            }
        }

        return bitmap;
    }

	public void resetSeen() {
		mSeen = false;		
		mSeenAtFirstColumn = 1000;
		mSeenAtLastColumn = 0;
		mSeenAtDistance = Float.MAX_VALUE;
	}
	
	public void hasBeenSeen() {
		mSeen = true;	
	}
	
	public void setSeenData(int castColumn, float distToGridBeingHit) {
		if(castColumn < mSeenAtFirstColumn) {
			mSeenAtFirstColumn = castColumn;
		}
		if(castColumn > mSeenAtLastColumn) {
			mSeenAtLastColumn = castColumn;
		}
		if(distToGridBeingHit < mSeenAtDistance) {
			mSeenAtDistance = distToGridBeingHit;
		}
	
	}

	public void startOpen() {
		mState = DOOR_OPENING;
		mWidthOfDoorShowing = TEXTURE_WIDTH;		
	}
	
	public void startClose() {
		mState = DOOR_CLOSING;
		mWidthOfDoorShowing = 0;		
	}
	
	public void doOpen() {
		mWidthOfDoorShowing = mWidthOfDoorShowing - mOpenCloseSpeed;
		if(mWidthOfDoorShowing == 0) {
			mState = DOOR_OPEN;
		}
	}
	
	public void doClose() {
		mWidthOfDoorShowing = mWidthOfDoorShowing + mOpenCloseSpeed;
		if(mWidthOfDoorShowing == TEXTURE_WIDTH) {
			mState = DOOR_CLOSED;
		}
	}
	
	public void destroy() {
        for (int x = 0; x < mBitmaps.length; x++) {
        	if(mBitmaps[x] != null) {
        		mBitmaps[x].recycle();
        		mBitmaps[x] = null;
        	}
        }
	}
	
	public int getSeenAtFirstColumn() {
		return mSeenAtFirstColumn;
	}
	
	public int getSeenAtLastColumn() {
		return mSeenAtLastColumn;
	}
	
	public int getHeight() {
		return DOOR_HEIGHT;
	}
	
	public int getWidth() {
		return DOOR_WIDTH;
	}
	
	public Bitmap getCurrentBitmap() {
		return mBitmaps[0];
	}

	public float getDistance() {
		return mSeenAtDistance;
	}
	
	public int getType() {
		return mDoorType;
	}
	
	/**
	 * This is only called if the door is opening or closing
	 * (if open it is not drawn at all, if closed it is drawn as a wall)
	 * @return
	 */
	public Rect getSourceRect() {
		return new Rect(0,0,mWidthOfDoorShowing,TEXTURE_HEIGHT);
	}

	public int getAmountOpen() {
		return mWidthOfDoorShowing;
	}

	public boolean isExitDoor() {
		return mIsExit;
	}

	public void reset() {
		mState = DOOR_CLOSED;
		mWidthOfDoorShowing = TEXTURE_WIDTH;
	}
}